<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="70px" class="queryForms">
			<el-form-item label="设备名称" prop="deviceName">
				<el-input v-model="queryParams.deviceName" placeholder="请输入设备名称" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit  />
			</el-form-item>
			<el-form-item label="设备型号" prop="deviceModel">
				<el-input v-model="queryParams.deviceModel" placeholder="请输入设备型号" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit  />
			</el-form-item>
			<el-form-item label="设备编号" prop="deviceNumber">
				<el-input v-model="queryParams.deviceNumber" placeholder="请输入设备编号" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit  />
			</el-form-item>
			<el-form-item label="购置日期" prop="purchaseTime">
				<el-date-picker v-model="queryParams.purchaseTime" type="date" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择日期"> </el-date-picker>
			</el-form-item>
			<el-form-item label="设备类型" prop="deviceTypeId">
				<el-select v-model="queryParams.deviceTypeId" placeholder="请选择">
					<el-option v-for="item in deviceTypeList" :key="item.id" :label="item.deviceType" :value="item.id"> </el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="完好状态" prop="intactState">
				<el-select v-model="queryParams.intactState" placeholder="请选择">
					<el-option v-for="dict in dict.type.product_intact_state" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['erp:device:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['erp:device:remove']">删除</el-button>
			</el-col>
			<CreateForm :ids="ids" formtype="t_device" />
			<right-toolbar @changeTableData="changeTableData" menuNames="device13" :showSearch.sync="showSearch" @queryTable="getList" :columns.sync="columns" perm="equipment:produce:device:index"></right-toolbar>
		</el-row>

		<div :class="sidebar.opened && eleTabTop <= 85 ? 'eleTab' : !sidebar.opened && eleTabTop <= 85 ? 'eleTabs' : ''" class="informationTab" ref="eleTab">
			<el-table ref="table" @row-click="row_click" v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange" border height="100%">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="序号" align="center" type="index" width="55" />
				<el-table-column label="设备类型" prop="deviceTypeName" min-width="150" :show-overflow-tooltip="true" v-if="columns[0].visible" />
				<el-table-column label="设备名称" prop="deviceName" min-width="150" :show-overflow-tooltip="true" v-if="columns[1].visible" />
				<el-table-column label="设备编号" prop="deviceNumber" min-width="150" :show-overflow-tooltip="true" v-if="columns[2].visible" />
				<el-table-column label="资产编号" prop="propertyNumber" min-width="150" :show-overflow-tooltip="true" v-if="columns[3].visible" />
				<el-table-column label="设备型号" prop="deviceModel" min-width="150" :show-overflow-tooltip="true" v-if="columns[4].visible" />
				<el-table-column label="生产日期" align="center" prop="producedTime" min-width="150" :show-overflow-tooltip="true" v-if="columns[5].visible">
					<template slot-scope="scope">
						<span>{{ parseTime(scope.row.producedTime, "{y}-{m}-{d}") }}</span>
					</template>
				</el-table-column>
				<el-table-column label="购置日期" align="center" prop="purchaseTime" min-width="150" :show-overflow-tooltip="true" v-if="columns[6].visible">
					<template slot-scope="scope">
						<span>{{ parseTime(scope.row.purchaseTime, "{y}-{m}-{d}") }}</span>
					</template>
				</el-table-column>
				<el-table-column label="完好状态" align="center" prop="intactState" min-width="150" :show-overflow-tooltip="true" v-if="columns[7].visible">
					<template slot-scope="scope">
						<dict-tag :options="dict.type.product_intact_state" :value="scope.row.intactState" />
					</template>
				</el-table-column>
				<el-table-column label="安装/存放位置" min-width="150" prop="placeStorage" :show-overflow-tooltip="true" v-if="columns[8].visible" />
				<el-table-column label="生产厂及国别" prop="manufacturingPlant" min-width="150" :show-overflow-tooltip="true" v-if="columns[9].visible" />
				<el-table-column label="数量" align="center" prop="deviceQuantity" min-width="150" :show-overflow-tooltip="true" v-if="columns[10].visible" />
				<el-table-column label="价值" prop="cost" min-width="150" :show-overflow-tooltip="true" v-if="columns[11].visible" />
				<!-- <el-table-column label="备注" align="center" prop="remark" /> -->
				<el-table-column label="操作" class-name="small-padding fixed-width" width="240px" fixed="right" align="center">
					<template slot-scope="scope">
						<el-button size="mini" type="text" icon="el-icon-guide" v-hasPermi="['erp:device:rule']" @click="handleRule(scope.row)">保养规则</el-button>
						<el-button size="mini" type="text" icon="el-icon-set-up" v-hasPermi="['erp:device:content']" @click="handleJump2(scope.row)">保养内容</el-button>
						<!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['erp:device:edit']">修改</el-button>
						<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['erp:device:remove']">删除</el-button>
						<el-button size="mini" type="text" icon="el-icon-wallet" @click="handleJump3(scope.row)" v-hasPermi="['erp:device:annex']">附件</el-button> -->
						<el-dropdown size="mini" @command="command => handleCommand(command, scope.row)" v-hasPermi="['erp:device:edit', 'erp:device:remove', 'erp:device:annex']">
							<el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
							<el-dropdown-menu slot="dropdown">
								<el-dropdown-item command="handleUpdate" v-hasPermi="['erp:device:edit']">修改</el-dropdown-item>
								<el-dropdown-item command="handleDelete" v-hasPermi="['erp:device:remove']">删除</el-dropdown-item>
								<el-dropdown-item command="handleJump3" v-hasPermi="['erp:device:annex']">附件</el-dropdown-item>
							</el-dropdown-menu>
						</el-dropdown>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<pagination menuNames="Pagination15" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改设备对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="900px" append-to-body v-dialogDrag>
			<el-form ref="form" class="add-form2" :model="form" :rules="rules" label-width="115px">
				<el-form-item label="设备类型" prop="deviceTypeId">
					<el-select v-model="form.deviceTypeId" placeholder="请选择" @change="changeDeviceType">
						<el-option v-for="item in deviceTypeList" :key="item.id" :label="item.deviceType" :value="item.id" :disabled="item.state == '0'"> </el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="设备名称" prop="deviceName">
					<el-input v-model="form.deviceName" placeholder="请输入设备名称" maxlength="20" show-word-limit  />
				</el-form-item>
				<el-form-item label="设备编号" prop="deviceNumber">
					<el-input v-model="form.deviceNumber" placeholder="请输入设备编号" maxlength="20" show-word-limit  />
				</el-form-item>
				<el-form-item label="设备型号" prop="deviceModel">
					<el-input v-model="form.deviceModel" placeholder="请输入设备型号" maxlength="20" show-word-limit  />
				</el-form-item>
				<el-form-item label="资产编号" prop="propertyNumber">
					<el-input v-model="form.propertyNumber" placeholder="请输入资产编号" maxlength="20" show-word-limit  />
				</el-form-item>
				<el-form-item label="生产日期" prop="producedTime">
					<el-date-picker clearable v-model="form.producedTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择生产日期"> </el-date-picker>
				</el-form-item>
				<el-form-item label="购置日期" prop="purchaseTime">
					<el-date-picker clearable v-model="form.purchaseTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择购置日期"> </el-date-picker>
				</el-form-item>
				<el-form-item label="完好状态" prop="intactState">
					<el-select v-model="form.intactState" placeholder="请选择">
						<el-option v-for="dict in dict.type.product_intact_state" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="安装/存放位置" prop="placeStorage">
					<el-input v-model="form.placeStorage" placeholder="请输入内容" maxlength="20" show-word-limit  />
				</el-form-item>
				<el-form-item label="生产厂及国别" prop="manufacturingPlant">
					<el-input v-model="form.manufacturingPlant" placeholder="请输入内容" maxlength="20" show-word-limit  />
				</el-form-item>
				<el-form-item label="数量" prop="deviceQuantity">
					<el-input v-model="form.deviceQuantity" disabled placeholder="请输入数量" />
				</el-form-item>
				<el-form-item label="价值" prop="cost">
					<el-input-number v-model="form.cost" placeholder="请输入价值" :min="0" :max="99999999" :precision="2" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>

		<!-- 设备保养规则 -->
		<el-dialog title="设备保养规则" :visible.sync="ruleopen" width="900px" append-to-body v-dialogDrag>
			<MonthLevel :ruleMonths="ruleMonths" />
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitRule">确 定</el-button>
				<el-button @click="cancelRule">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { listDevice, getDevice, delDevice, addDevice, updateDevice } from "@/api/equipment/produce/device"
import { getDeviceTypeList,getcolumn} from "@/api/selectList"
import { setRuleAct, listDeviceMaintenanceRule } from "@/api/equipment/produce/deviceRules"
import MonthLevel from "@/views/components/MonthLevel"
import CreateForm from "@/views/components/CreateForm"
import { getbind, menulist, OperationVideoMenuList } from "@/api/fileVedio/fileVedios"
import { mapGetters } from "vuex"
import DocumentShow from "@/views/components/DocumentShow"
export default {
	name: "Device",
	dicts: ["product_intact_state"],
	components: { MonthLevel, CreateForm, DocumentShow },
	computed: {
		...mapGetters(["sidebar"])
	},
	data() {
		return {
			fileVedioList: [],
			showfileListDialog: false,
			showTodoListDialog: false,
			eleTabTop: 0, //表格容器距浏览器顶部距离
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 设备表格数据
			deviceList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			ruleopen: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				deviceTypeId: undefined,
				deviceName: undefined,
				intactState: undefined,
				deviceModel: undefined,
				deviceNumber: undefined,
				purchaseTime: undefined
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
				deviceTypeId: [{ required: true, message: "请选择设备类型", trigger: "blur" }],
				deviceName: [{ required: true, message: "请输入设备名称", trigger: "blur" }]
			},
			// 设备类型列表
			deviceTypeList: [],
			// 设备保养规则12个月
			ruleMonths: [],
			// 列信息
			columns: [
				{ key: 0, label: `设备类型`, visible: true },
				{ key: 1, label: `设备名称`, visible: true },
				{ key: 2, label: `设备编号`, visible: true },
				{ key: 3, label: `资产编号`, visible: true },
				{ key: 4, label: `设备型号`, visible: true },
				{ key: 5, label: `生产日期`, visible: true },
				{ key: 6, label: `购置日期`, visible: true },
				{ key: 7, label: `完好状态`, visible: true },
				{ key: 8, label: `安装/存放位置`, visible: true },
				{ key: 9, label: `生产厂及国别`, visible: true },
				{ key: 10, label: `数量`, visible: true },
				{ key: 11, label: `价值`, visible: true }
			]
		}
	},
	created() {
		this.getDeviceTypeList()
		this.getSize()
	},
	mounted() {
		// 滚动条的获取
		this.handleScrollx()
		window.addEventListener("scroll", this.handleScrollx, true)
	},
	activated() {
			this.getDeviceTypeList()
		this.getSize()
		//获取表格容器距页面头部的距离
		this.handleScrollx()
	},
	destroyed() {
		window.removeEventListener("scroll", this.handleScrollx, true)
	},
	methods: {
    getSize() {
			getcolumn({ pageName: "Pagination15" }).then(response => {
				this.queryParams.pageSize = response.data.length ? (response.data[0].limit ? response.data[0].limit : 10) : 10
				this.getList()
			})
		},
		// 单击表格行
		row_click(row, column, event) {
			if (column && column.label == "操作") {
				return
			} else {
				this.$refs.table.toggleRowSelection(row)
			}
		},
		changeTableData() {
			this.$nextTick(() => {
				this.$refs.table.doLayout()
			})
		},

		//获取表格容器距页面头部的距离
		handleScrollx() {
			this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top
		},
		// 查询设备类型列表
		async getDeviceTypeList() {
			let ret = await getDeviceTypeList()
			if (ret && ret.code == 200) {
				this.deviceTypeList = ret.rows
			}
		},
		/** 查询设备列表 */
		getList() {
			console.log(this.queryParams)
			this.loading = true
			listDevice(this.queryParams).then(response => {
				this.deviceList = response.rows
				this.total = response.total
				this.loading = false
			})
		},
		// 更改设备类型
		changeDeviceType() {
			let cur = this.deviceTypeList.find(item => {
				return item.id == this.form.deviceTypeId
			})
			this.form.deviceTypeName = cur.deviceType
		},
		// 取消按钮
		cancel() {
			this.open = false
			this.reset()
		},
		// 表单重置
		reset() {
			this.form = {
				id: undefined,
				deviceTypeId: undefined,
				deviceTypeName: undefined,
				deviceName: undefined,
				deviceNumber: undefined,
				propertyNumber: undefined,
				deviceModel: undefined,
				producedTime: undefined,
				purchaseTime: undefined,
				intactState: undefined,
				placeStorage: undefined,
				manufacturingPlant: undefined,
				deviceQuantity: 1,
				cost: undefined,
				remark: undefined,
				delFlag: undefined,
				createBy: undefined,
				createTime: undefined,
				updateBy: undefined,
				updateTime: undefined
			}
			this.resetForm("form")
		},
		async resetRuleMonth(id) {
			let ruleMonths = [
				{ month: "1", monthName: "一月", deviceId: id },
				{ month: "2", monthName: "二月", deviceId: id },
				{ month: "3", monthName: "三月", deviceId: id },
				{ month: "4", monthName: "四月", deviceId: id },
				{ month: "5", monthName: "五月", deviceId: id },
				{ month: "6", monthName: "六月", deviceId: id },
				{ month: "7", monthName: "七月", deviceId: id },
				{ month: "8", monthName: "八月", deviceId: id },
				{ month: "9", monthName: "九月", deviceId: id },
				{ month: "10", monthName: "十月", deviceId: id },
				{ month: "11", monthName: "十一月", deviceId: id },
				{ month: "12", monthName: "十二月", deviceId: id }
			]
			let ret = await listDeviceMaintenanceRule({ deviceId: id, pageSize: 100 })
			ret.rows.forEach(item => {
				ruleMonths.forEach((item1, index1) => {
					if (item.month == item1.month) {
						item1.id = item.id
						item1.maintenanceLevelId = item.maintenanceLevelId
					}
				})
			})
			this.ruleMonths = ruleMonths
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1
			this.getList()
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm")
			this.handleQuery()
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		// 更多操作触发
		handleCommand(command, row) {
			this[command](row)
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset()
			this.open = true
			this.title = "添加设备"
			this.getDeviceTypeList()
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset()
			const id = row.id || this.ids
			getDevice(id).then(response => {
				this.form = response.data
				this.open = true
				this.title = "修改设备"
			})
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.id != undefined) {
						updateDevice(this.form).then(response => {
							this.$modal.msgSuccess("修改成功")
							this.open = false
							this.getList()
						})
					} else {
						addDevice(this.form).then(response => {
							this.$modal.msgSuccess("新增成功")
							this.open = false
							this.getList()
						})
					}
				}
			})
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids
			this.$modal
				.confirm("是否确认删除数据项？")
				.then(function () {
					return delDevice(ids)
				})
				.then(() => {
					this.getList()
					this.$modal.msgSuccess("删除成功")
				})
				.catch(() => {})
		},
		// 跳转到保养规则
		handleRule(row) {
			this.resetRuleMonth(row.id)
			this.ruleopen = true
		},
		submitRule() {
			// let list = this.ruleMonths.filter((item) => {
			//   return item.maintenanceLevelId;
			// });
			let data = []
			this.ruleMonths.forEach(item => {
				data.push({
					deviceId: item.deviceId,
					maintenanceLevelId: item.maintenanceLevelId,
					month: item.month,
					id: item.id
				})
			})
			setRuleAct(data).then(() => {
				this.$modal.msgSuccess("操作成功")
				this.open = false
			})
			this.ruleopen = false
		},
		cancelRule() {
			this.ruleopen = false
		},
		// 跳转到保养内容
		handleJump2(row) {
			let url = "/equipment/produce/deviceContent?id=" + row.id
			this.$router.push(url)
		},
		// 跳转到附件
		handleJump3(row) {
			this.$router.push({
				path: "/attachment/attachmentUpload",
				query: {
					TableId: row.id,
					typeId: 1,
					perm: "erp:device:annex"
				}
			})
		}
		/** 导出按钮操作 */
		// handleExport() {
		//   this.download(
		//     "erp/device/export",
		//     {
		//       ...this.queryParams,
		//     },
		//     `device_${new Date().getTime()}.xlsx`
		//   );
		// },
	}
}
</script>
<style scss scoped>
::v-deep .el-table__fixed-header-wrapper .fixed-width {
	width: 240px !important;
}
</style>
